<template>
  <div class="">
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="160px" v-loading="loadingForm">
      <div class="header-btn">
        <div>回收规则</div>
        <el-button type="primary" @click="sure">保存</el-button>
      </div>
      <div class="main">
        <div class="biao">分配及离职未继承客户，符合以下规则系统将在每天01:00:00自动回收</div>
        <div class="title">
          <span style="width: 100px">回收时间配置</span>
          <span style="font-size: 12px;width: 160px; ;"
          >(回收规则以领取时配置为准)</span></div>

        <el-row>
          <el-col :span="3">
            <el-form-item label="是否启用" prop="noFriendisOpen">
              <el-switch v-model="formData.noFriendisOpen"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="领取超过:" prop="noFriend">
              <el-select v-model="formData.noFriend" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              天
              <span>未加好友</span>
              <el-radio-group v-model="formData.noFriendTime" size="medium">
                <el-radio v-for="(item, index) in radioOptions" :key="index" :label="item.value" :disabled="item.disabled">
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="3">
            <el-form-item label="是否启用" prop="noFollowisOpen">
              <el-switch v-model="formData.noFollowisOpen"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="领取超过:" prop="noFollow">
              <el-select v-model="formData.noFollow" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              天
              <span>未跟进</span>
              <el-radio-group v-model="formData.noFollowTime" size="medium">
                <el-radio v-for="(item, index) in radioOptions" :key="index" :label="item.value" :disabled="item.disabled">
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="3">
            <el-form-item label="是否启用" prop="noFollowStateisOpen">
              <el-switch v-model="formData.noFollowStateisOpen"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="领取超过:" prop="noFollowState">
              <el-select v-model="formData.noFollowState" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              天
              <span>阶段未变化</span>
              <el-radio-group v-model="formData.noFollowStateTime" size="medium">
                <el-radio v-for="(item, index) in radioOptions" :key="index" :label="item.value" :disabled="item.disabled">
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>



        <!--        <el-form-item label="领取超过:" prop="noSigned">-->
        <!--          <el-select v-model="formData.noSigned" placeholder="请选择">-->
        <!--            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--          天-->
        <!--          <span>未成单</span>-->
        <!--          <el-radio-group v-model="formData.noSignedTime" size="medium">-->
        <!--            <el-radio v-for="(item, index) in radioOptions" :key="index" :label="item.value" :disabled="item.disabled">-->
        <!--              {{ item.label }}-->
        <!--            </el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->

        <el-row>
          <el-col :span="3">
            <el-form-item label="是否启用" prop="noInheritisOpen">
              <el-switch v-model="formData.noInheritisOpen"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="21">
            <el-form-item label="离职超过:" prop="noInherit">
              <el-select v-model="formData.noInherit" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              天
              <span>未继承</span>
              <el-radio-group v-model="formData.noInheritTime" size="medium">
                <el-radio v-for="(item, index) in radioOptions" :key="index" :label="item.value" :disabled="item.disabled">
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>


        <div class="title">回收预警时间</div>
        <el-form-item label="临近回收时间:" prop="nearRecovery">
          <el-input v-model="formData.nearRecovery"></el-input>
          天
          <span></span>
          <el-radio-group v-model="formData.nearRecoveryTime" size="medium">
            <el-radio v-for="(item, index) in radioOptions" :key="index" :label="item.value" :disabled="item.disabled">
              {{ item.label }}
            </el-radio>
          </el-radio-group>
          <div class="remarks">需录入非零整数</div>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<script>
import { addRule, selectRecoveryRule } from '@/api/customer/rules'

export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        isOpen: false,
        noFriendisOpen: false,
        noFriend: undefined,
        noFriendTime: 0,
        noFollowisOpen: false,
        noFollow: undefined,
        noFollowTime: 0,
        noFollowStateisOpen: false,
        noFollowState: undefined,
        noFollowStateTime: 0,
        noSigned: undefined,
        noSignedTime: 0,
        noInheritisOpen: false,
        noInherit: undefined,
        noInheritTime: 0,
        nearRecovery: undefined,
        nearRecoveryTime: 0
      },
      loadingForm: false,
      rules: {
        noFriend: [
          { required: true, message: '请选择未加好友时间配置', trigger: 'change' }
        ],
        noFollow: [
          { required: true, message: '请选择未跟进时间配置', trigger: 'change' }
        ],
        noFollowState: [
          { required: true, message: '请选择阶段未变化时间配置', trigger: 'change' }
        ],
        noSigned: [
          { required: true, message: '请选择未成单时间配置', trigger: 'change' }
        ],
        noInherit: [
          { required: true, message: '请选择未继承时间配置', trigger: 'change' }
        ],
        nearRecovery: [
          { validator: this.validatePass, trigger: 'blur' }
        ]
      },
      radioOptions: [{
        'label': '按照工作日',
        'value': 0
      }, {
        'label': '按照自然日',
        'value': 1
      }],
      options: [
        {
          value: '7',
          label: '7'
        }, {
          value: '30',
          label: '30'
        }, {
          value: '60',
          label: '60'
        }, {
          value: '90',
          label: '90'
        }, {
          value: '180',
          label: '180'
        }, {
          value: '365',
          label: '365'
        }
      ]
    }
  },
  computed: {
    userName() {
      return this.$store.state.user.userName
    }
  },
  watch: {},
  created() {
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getInfo()
    },
    getInfo() {
      // const params = {
      //   createBy: this.userName,
      // };
      this.loadingForm = true
      selectRecoveryRule().then((res) => {
        if (res.code === 200) {
          // console.log(res);
          if (res.data.length === 0) return
          let list = res.data[0]
          const recoveryRule = JSON.parse(list.recoveryRule)
          this.formData = recoveryRule
          this.formData.isOpen = recoveryRule.isOpen === 0 ? false : true
          this.formData.noFriendisOpen = recoveryRule.noFriendisOpen === 0 ? false : true
          this.formData.noFollowisOpen = recoveryRule.noFollowisOpen === 0 ? false : true
          this.formData.noInheritisOpen = recoveryRule.noInheritisOpen === 0 ? false : true
          this.formData.noFollowStateisOpen = recoveryRule.noFollowStateisOpen === 0 ? false : true

          console.log(list)
        }
      }).catch(() => {
        console.log('error')
      }).finally(() => {
        console.log('结束')
        this.loadingForm = false
      })
    },
    validatePass(rule, value, callback) {
      if (/\d/.test(value) && value % 1 == 0 && value != 0) {
        callback()
      } else {
        callback(new Error('请录入非零整数'))
      }
    },
    reset() {
      this.formData = {
        isOpen: false,
        noFriend: undefined,
        noFriendTime: undefined,
        noFollow: undefined,
        noFollowTime: undefined,
        noFollowState: undefined,
        noFollowStateTime: undefined,
        noSigned: undefined,
        noSignedTime: undefined,
        noInherit: undefined,
        noInheritTime: undefined,
        nearRecovery: undefined,
        nearRecoveryTime: undefined
      }
    },
    sure() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        // TODO 提交问卷
        const params = {
          recoveryRule: {
            ...this.formData,
            isOpen: +this.formData.isOpen,
            noFriendisOpen: +this.formData.noFriendisOpen,
            noFollowisOpen: +this.formData.noFollowisOpen,
            noInheritisOpen: +this.formData.noInheritisOpen,
            noFollowStateisOpen: +this.formData.noFollowStateisOpen

          }
        }

        console.log(params, '..........')
        addRule(params).then((res) => {
          if (res.code === 200) {
            this.$message.success('回收规则保存成功')
            // this.reset();
          }
        }).catch(() => {

        })
      })
    }
  }
}

</script>
<style lang="scss" scoped>
@import "@/styles/base.scss";

.header-btn {
  @include headerBtn;
}

.title {
  padding: 15px 0;
  font-size: 16px;
}

.main {
  background: #fff;
  padding: 20px 20px 20px 20px;

  span {
    width: 80px;
    display: inline-block;
    margin-left: 10px;
  }

  .el-select {
    margin-right: 10px;
    width: 10%;
  }

  .el-input {
    width: 10%;
    margin-right: 10px;
  }

  .remarks {
    font-size: 12px;
    color: #082aff;
    line-height: 20px;
    width: 10%;
    text-align: right;
  }
}

.biao {
  padding: 20px 10px;
  background: #EDF1F2;
  width: 50%;
  border-radius: 20px;
}
</style>
